package cn.iocoder.yudao.module.famous.doctor.controller.admin.patientaudit;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.patientaudit.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.patientaudit.PatientAuditDO;
import cn.iocoder.yudao.module.famous.doctor.convert.patientaudit.PatientAuditConvert;
import cn.iocoder.yudao.module.famous.doctor.service.patientaudit.PatientAuditService;

@Tag(name = "管理后台 - 临床研究患者审批")
@RestController
@RequestMapping("/famous.doctor/patient-audit")
@Validated
public class PatientAuditController {

    @Resource
    private PatientAuditService patientAuditService;

    @PostMapping("/create")
    @Operation(summary = "创建临床研究患者审批")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:create')")
    public CommonResult<Integer> createPatientAudit(@Valid @RequestBody PatientAuditCreateReqVO createReqVO) {
        return success(patientAuditService.createPatientAudit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新临床研究患者审批")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:update')")
    public CommonResult<Boolean> updatePatientAudit(@Valid @RequestBody PatientAuditUpdateReqVO updateReqVO) {
        patientAuditService.updatePatientAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除临床研究患者审批")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:delete')")
    public CommonResult<Boolean> deletePatientAudit(@RequestParam("id") Integer id) {
        patientAuditService.deletePatientAudit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得临床研究患者审批")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:query')")
    public CommonResult<PatientAuditRespVO> getPatientAudit(@RequestParam("id") Integer id) {
        PatientAuditDO patientAudit = patientAuditService.getPatientAudit(id);
        return success(PatientAuditConvert.INSTANCE.convert(patientAudit));
    }

    @GetMapping("/list")
    @Operation(summary = "获得临床研究患者审批列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:query')")
    public CommonResult<List<PatientAuditRespVO>> getPatientAuditList(@RequestParam("ids") Collection<Integer> ids) {
        List<PatientAuditDO> list = patientAuditService.getPatientAuditList(ids);
        return success(PatientAuditConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得临床研究患者审批分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:query')")
    public CommonResult<PageResult<PatientAuditRespVO>> getPatientAuditPage(@Valid PatientAuditPageReqVO pageVO) {
        PageResult<PatientAuditDO> pageResult = patientAuditService.getPatientAuditPage(pageVO);
        return success(PatientAuditConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出临床研究患者审批 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:patient-audit:export')")
    @OperateLog(type = EXPORT)
    public void exportPatientAuditExcel(@Valid PatientAuditExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PatientAuditDO> list = patientAuditService.getPatientAuditList(exportReqVO);
        // 导出 Excel
        List<PatientAuditExcelVO> datas = PatientAuditConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "临床研究患者审批.xls", "数据", PatientAuditExcelVO.class, datas);
    }

}
